package com.isdepci.project.carbon.service.impl;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.BWeldHeatEmission;
import com.isdepci.project.carbon.domain.BWeldHeatEmissionParameter;
import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.carbon.mapper.BWeldHeatEmissionMapper;
import com.isdepci.project.carbon.mapper.BWeldHeatEmissionParameterMapper;
import com.isdepci.project.carbon.service.IBWeldHeatEmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 切焊加热Service业务层处理
 *
 * @author isdepci
 * @date 2025-03-29
 */
@Service
public class BWeldHeatEmissionServiceImpl implements IBWeldHeatEmissionService
{
    @Autowired
    private BWeldHeatEmissionMapper bWeldHeatEmissionMapper;
    @Autowired
    private BWeldHeatEmissionParameterMapper emissionParameterMapper;


    @Override
    public BWeldHeatEmissionParameter getWeldHeatEmissionParameters(Integer activityType) {
        return emissionParameterMapper.findByActivityType(activityType);
    }

    @Override
    public AjaxResult calWeldHeatEmission(Long projectId) {
        // 查询出所有与项目ID相关的BWeldHeatEmission数据
        List<BWeldHeatEmission> bWeldHeatEmissions = bWeldHeatEmissionMapper.selectBWeldHeatEmissionByProjectId(projectId);
        // 初始化总的切焊加热量
        BigDecimal totalWeldHeatEmission = BigDecimal.ZERO;
        // 遍历每一条数据，进行计算
        for (BWeldHeatEmission bWeldHeatEmission : bWeldHeatEmissions) {
            // 计算AD = (weightBefore - weightAfter) * totalBottles * totalActivities
            BigDecimal ad = bWeldHeatEmission.getWeightBefore()
                    .subtract(bWeldHeatEmission.getWeightAfter()) // (weightBefore - weightAfter)
                    .multiply(new BigDecimal(bWeldHeatEmission.getTotalBottles())) // * totalBottles
                    .multiply(new BigDecimal(bWeldHeatEmission.getTotalActivities())); // * totalActivities

            // 计算E切焊加热 = AD * emissionFactor * burnEfficiency * purity
            BigDecimal weldHeatEmission = ad
                    .multiply(bWeldHeatEmission.getEmissionFactor()) // * emissionFactor
                    .multiply(bWeldHeatEmission.getBurnEfficiency().divide(new BigDecimal("100")))
                    .multiply(bWeldHeatEmission.getPurity().divide(new BigDecimal("100")));
            bWeldHeatEmission.setCarbonEmissions(weldHeatEmission);
            bWeldHeatEmissionMapper.updateBWeldHeatEmission(bWeldHeatEmission);
            // 累加E切焊加热
            totalWeldHeatEmission = totalWeldHeatEmission.add(weldHeatEmission);
        }
        // 返回总的切焊加热量
        return AjaxResult.success(totalWeldHeatEmission.setScale(2, RoundingMode.HALF_UP));
    }

    public AjaxResult changeDataByProjectId(WeatherDataExcel excel) {
        List<BWeldHeatEmission> list =  excel.getWeldHeatEmissionList();
        list.forEach(item->{
            if(excel.getProjectId()!=null){
                item.setProjectId(excel.getProjectId());
            }
        });
        if(list.size()>0){
            bWeldHeatEmissionMapper.insertList(list);
        }
        return AjaxResult.success();
    }



    /**
     * 查询切焊加热
     */
    @Override
    public BWeldHeatEmission selectBWeldHeatEmissionById(Long id)
    {
        return bWeldHeatEmissionMapper.selectBWeldHeatEmissionById(id);
    }

    /**
     * 查询切焊加热列表
     */
    @Override
    public List<BWeldHeatEmission> selectBWeldHeatEmissionList(BWeldHeatEmission bWeldHeatEmission)
    {
        return bWeldHeatEmissionMapper.selectBWeldHeatEmissionList(bWeldHeatEmission);
    }

    /**
     * 新增切焊加热
     */
    @Override
    public int insertBWeldHeatEmission(BWeldHeatEmission bWeldHeatEmission)
    {
        return bWeldHeatEmissionMapper.insertBWeldHeatEmission(bWeldHeatEmission);
    }

    /**
     * 修改切焊加热
     */
    @Override
    public int updateBWeldHeatEmission(BWeldHeatEmission bWeldHeatEmission)
    {
        return bWeldHeatEmissionMapper.updateBWeldHeatEmission(bWeldHeatEmission);
    }

    /**
     * 批量删除切焊加热
     */
    @Override
    public int deleteBWeldHeatEmissionByIds(Long[] ids)
    {
        return bWeldHeatEmissionMapper.deleteBWeldHeatEmissionByIds(ids);
    }

    /**
     * 删除切焊加热信息
     */
    @Override
    public int deleteBWeldHeatEmissionById(Long id)
    {
        return bWeldHeatEmissionMapper.deleteBWeldHeatEmissionById(id);
    }


}
